//
//  XMInt+Extension.swift
//  AnneKit-Swift
//
//  Created by 寜小陌 on 2021/3/3.
//  Copyright © 2021 宁小陌. All rights reserved.
//

import UIKit

extension Int {
    /// Int类型的分，转为元为单位的金额 《isNeedUnit 是否需要¥单位》
    public func nxm_toYuanRMB(_ isNeedUnit:Bool = true) -> String {
        // 分为单位
        let figure = self
        
        let price = CGFloat(figure)
        let price1 = String(format: "%.2f", CGFloat(figure)/100)
        var price2 = String(format: "%.2f", CGFloat(figure)/100)
        let indexOne = price1.index(price1.endIndex, offsetBy: -1)
        let lastOne = price1.suffix(from: indexOne)
        let indexTwo = price1.index(price1.endIndex, offsetBy: -2)
        let lastTwo = price1.suffix(from: indexTwo)
        if lastOne == "0" {
            price2 = String.init(format: "%.1f", price/100)
        }
        if lastTwo == "00" {
            price2 = String.init(format: "%.0f", price/100)
        }
        
        return isNeedUnit ? ("¥" + price2) : price2
    }
    // MARK: - Int保留多少位 默认为2
    public func nxm_keepInt(_ digits:Int = 2) -> String {
        let formatter = NumberFormatter()
        formatter.minimumIntegerDigits = digits
        return String.nxm_getString(formatter.string(for: self))
    }
}

//MARK: 类型转换
extension Int {
    /// Int 转 字符串
    public var nxm_stringValue: String {
        get {
            return String(self)
        }
    }
    
    /// Int 转 Float
    public var nxm_folatValue: Float {
        get {
            return Float(self)
        }
    }
    
    /// Int 转 Double
    public var nxm_doubleValue: Double {
        get {
            return Double(self)
        }
    }
    
    /// Int 转 NSNumber
    public var nxm_numberValue: NSNumber {
        get {
            return NSNumber.init(value: self)
        }
    }
    
}
